package dk.hewison.client.menu;

import dk.hewison.client.AppEvent;
import static dk.hewison.client.AppEvent.Event.PAGE_SELECTED;
import dk.hewison.client.Log;
import dk.hewison.client.MediaAppController;
import dk.hewison.client.mvc.AbstractController;
import dk.hewison.client.mvc.Action;
import dk.hewison.client.mvc.ObjectSelectAction;
import dk.hewison.client.mvc.View;
import dk.hewison.client.pages.MenuItem;
import dk.hewison.client.panels.ViewFactory;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-15 12:47:07 +0000 (Sun, 15 Feb 2009) $:
 * @version $Revision: 364 $:
 */
public class MainMenuController extends AbstractController<MenuItem[]> {

    public static final int START_PAGE = 0;
    public static final int ALL_ALBUMS = 1;
    public static final int AN_ALBUM = 2;
    public static final int SLIDESHOW = 10;


    public static String[][] pages = {
            {"" + START_PAGE, "Start Page", "The Front Page", "This Page is already selected"},
            {"" + ALL_ALBUMS, "Albums", "The Status Page", "This Page is already selected"},
    };


    public MainMenuController(int id) {
        super(id);
    }

    protected Action[] initActions() {
        Action[] actions = new Action[pages.length];
        for (int i = 0; i < pages.length; i++) {
            String[] page = pages[i];
            actions[i] = makePageAction(page);
        }
        return actions;
    }

    private Action makePageAction(String[] page) {
        final int pageId = java.lang.Integer.parseInt(page[0]);
        return new ObjectSelectAction<Integer>(pageId, this, page[1], page[2], page[3], pageId) {
            public void selected(Integer integer) {
                pageSelected(integer);
            }

            @Override
            public boolean isEnabled() {
                return MediaAppController.getInstance().getCurrentPageId() != pageId;
            }
        };
    }


    private void pageSelected(Integer pageId) {
        Log.debug("MainMenuController.pageSelected" + pageId);
        fireEvent(new AppEvent<Integer>(PAGE_SELECTED, pageId));
        //MediaAppController.getInstance().showPage(pageId);
    }

    public View<MenuItem[]> createView() {
        return ViewFactory.getInstance().createMainMenuView();
    }

    public MenuItem[] getModel() {
        return new MenuItem[0];
    }

    public void setModel(MenuItem[] data) {

    }
}
